package com.tjsinfo.tjsoa.workReport.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.DayFormat;
import com.tjsinfo.tjsoa.common.FileManage;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.workReport.bo.TResourceWorkReportManage;
import com.tjsinfo.tjsoa.workReport.vo.TResourceWorkReport;


public class WorkReportSaveAction extends Action {
	private TResourceWorkReportManage resourceWorkReportManage;
	
	

	public TResourceWorkReportManage getResourceWorkReportManage() {
		return resourceWorkReportManage;
	}



	public void setResourceWorkReportManage(
			TResourceWorkReportManage resourceWorkReportManage) {
		this.resourceWorkReportManage = resourceWorkReportManage;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			User user = SessionUser.getUser(request);
			
			//创建静态html
			Date date = new Date();
			String fileName = new Long(date.getTime()).toString()+".html";
			String path = "d:\\gongwen\\";
			path += "workReport\\";
			String content = request.getParameter("contest");
			content="<html><head><title> New Document </title>\r<script language=\"JavaScript\">\r<!--\r if (window.Event) document.captureEvents(Event.MOUSEUP);"+
			"function nocontextmenu() { event.cancelBubble = true; event.returnValue = false; return false;}"+
			"function norightclick(e) {if (window.Event){ if (e.which == 2 || e.which == 3)return false;}"+
			"else if (event.button == 2 || event.button == 3){event.cancelBubble = true;event.returnValue = false;return false;}}"+
			"document.oncontextmenu = nocontextmenu;document.onmousedown = norightclick;\r//--></script>\r</head>\r<body>"+content+"</body></html>";
			FileManage manage = new FileManage();
			manage.write(content,path,fileName);
			
			//写入数据库
			TResourceWorkReport tid=new TResourceWorkReport();
			Date dateT = new Date();
			dateT=DayFormat.getCurrentUtilDate();
			tid.setTitle(request.getParameter("title"));
			tid.setCreateDate(dateT);
			tid.setContent("workReport\\"+fileName);
			tid.setUserId(user.getUser_id());
			tid.setUserName(user.getUsername());
			tid.setOrderNo(15);
			tid.setState(0);
			resourceWorkReportManage.save(tid);
			
			page(request, user);
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}



	private void page(HttpServletRequest request, User user) {
		Integer pageSize=12;
		String pageStr=request.getParameter("pageNo");
		if(pageStr==null)pageStr="0";
		int page=Integer.valueOf(pageStr).intValue();
		PaginationSupport paginationSupport=resourceWorkReportManage.findOrderList(user.getUser_id(),page,pageSize);
		request.setAttribute("paginationSupport", paginationSupport);
	}
}
